package com.minyifang.wcspf.work.ui.user.presenter;

import com.google.gson.Gson;
import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.work.ui.home.model.CheckRecordBean;
import com.minyifang.wcspf.work.ui.home.model.SignDataBean;
import com.minyifang.wcspf.work.ui.user.view.DailySignView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class DailySignPresenter extends BasePresenter<DailySignView> {

    public void getCheckRecordList() {
        Map<String, Object> map = new HashMap<>();
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().getCheckRecordList(requestBody), respond -> {
            getView().showLoadingView();
            if (respond.result == 1) {
                String data = SafeUtils.decrypt(respond.data);
                LogUtil.e(data);
                SignDataBean signDataBean = new Gson().fromJson(data, SignDataBean.class);
                getView().onGetCheckRecordListSucceed(signDataBean);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }

    public void insertCheckRecord() {
        Map<String, Object> map = new HashMap<>();
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        getView().showLoadingView();
        addTask(RetrofitHelper.getInstance().getService().insertCheckRecord(requestBody), respond -> {
            getView().showLoadingView();
            if (respond.result == 1) {
                String data = SafeUtils.decrypt(respond.data);
                LogUtil.e(data);
                CheckRecordBean checkRecordBean = new Gson().fromJson(data, CheckRecordBean.class);
                getView().onCheckSucceed(checkRecordBean);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }
}
